package cn.com.lyb.flink.window;

import cn.com.lyb.flink.bean.WaterSensor;
import cn.com.lyb.flink.split.WaterSensorMapFunction;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class WindowProcessDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> sensorDS = env.socketTextStream("172.32.1.6", 1234).map(new WaterSensorMapFunction());

        KeyedStream<WaterSensor, String> keyBy = sensorDS.keyBy((KeySelector<WaterSensor, String>) waterSensor -> waterSensor.getId());

        WindowedStream<WaterSensor, String, TimeWindow> window = keyBy.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));
        // 窗口分配器
//        window.apply(new WindowFunction<WaterSensor, Object, String, TimeWindow>() {
//            /**
//             *
//             *
//             * @param s 分组的key
//             * @param timeWindow 窗口对象
//             * @param iterable 存的数据
//             * @param collector 采集器
//             * @throws Exception
//             */
//            @Override
//            public void apply(String s, TimeWindow timeWindow, Iterable<WaterSensor> iterable, Collector<Object> collector) throws Exception {
//
//            }
//        }).print();

        window.process(new ProcessWindowFunction<WaterSensor, Object, String, TimeWindow>() {
            /**
             *
             * @param s 分组的key
             * @param context 上下文
             * @param iterable 迭代器
             * @param collector 采集器
             * @throws Exception
             */
            @Override
            public void process(String s, ProcessWindowFunction<WaterSensor, Object, String, TimeWindow>.Context context, Iterable<WaterSensor> iterable, Collector<Object> collector) throws Exception {
                long start = context.window().getStart();
                long end = context.window().getEnd();
                String startFormat = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss.SSS");
                String endFormat = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss.SSS");
                long cnt = iterable.spliterator().estimateSize();
                collector.collect("key = "+ s +"窗口开始时间 +" + startFormat +"结束时间" + endFormat +"迭代器大小" + cnt);
            }
        }).print();


        env.execute();
    }
}
